package com.syl.starter.itools.framework.utils;

import com.syl.starter.itools.base.constants.BaseConstant;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Base64;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * gzip 工具类
 * @author syl
 * @create 2018-10-10 9:38
 **/
public class GzipUtils {
    private static final String PREFIX = "gzip:";

    /**
     * 压缩数据流
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] gzip(byte[] data) throws Exception {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        GZIPOutputStream gzip = new GZIPOutputStream(bos);
        gzip.write(data);
        gzip.finish();
        gzip.close();
        byte[] ret = bos.toByteArray();
        bos.close();
        return ret;
    }

    /**
     * 使用gzip 压缩 默认utf8编码
     * @param data
     * @return
     * @throws Exception
     */
    public static String gzip(String data) throws Exception{
        return gzip(data, BaseConstant.UTF8);
    }

    /**
     * 使用gzip 压缩
     * @param data
     * @param encoding 编码
     * @return
     * @throws Exception
     */
    public static String gzip(String data, String encoding) throws Exception{
        return Base64.getEncoder().encodeToString(gzip(data.getBytes(encoding)));
    }

    /**
     * 压缩后使用前缀标注  默认utf8编码
     * @param data
     * @return
     * @throws Exception
     */
    public static String gzipWithPrefix(String data) throws Exception{
        return gzipWithPrefix(data, BaseConstant.UTF8);
    }

    /**
     * 压缩后使用前缀标注
     * @param data
     * @param encoding 编码
     * @return
     * @throws Exception
     */
    public static String gzipWithPrefix(String data, String encoding) throws Exception{
        return PREFIX + gzip(data, encoding);
    }

    /**
     * 对压缩流进行解压缩
     * @param data
     * @return
     * @throws Exception
     */
    public static byte[] unGzip(byte[] data) throws Exception {
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        GZIPInputStream gzip = new GZIPInputStream(bis);
        byte[] buf = new byte[1024];
        int num = -1;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while ((num = gzip.read(buf, 0, buf.length)) != -1) {
            bos.write(buf, 0, num);
        }
        gzip.close();
        bis.close();
        byte[] ret = bos.toByteArray();
        bos.flush();
        bos.close();
        return ret;
    }

    /**
     * 对压缩字符串进行解压缩 默认utf8编码
     * @param data
     * @return
     * @throws Exception
     */
    public static String unGzip(String data) throws Exception{
        return unGzip(data, BaseConstant.UTF8);
    }

    /**
     * 对压缩字符串进行解压缩
     * @param data
     * @param encoding 编码
     * @return
     * @throws Exception
     */
    public static String unGzip(String data, String encoding) throws Exception{
        return new String(unGzip(Base64.getDecoder().decode(data)), encoding);
    }

    /**
     * 对带前缀的压缩字符串进行解压缩 默认utf8编码
     * @param data
     * @return
     * @throws Exception
     */
    public static String unGzipWithPrefix(String data) throws Exception{
        return unGzipWithPrefix(data.replace(PREFIX, BaseConstant.EMPTY_STRING), BaseConstant.UTF8);
    }

    /**
     * 对带前缀的压缩字符串进行解压缩
     * @param data
     * @param encoding 编码
     * @return
     * @throws Exception
     */
    public static String unGzipWithPrefix(String data, String encoding) throws Exception{
        return unGzip(data.replace(PREFIX, BaseConstant.EMPTY_STRING), encoding);
    }

}
